<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/7
 * Time: 22:18
 */

namespace Xcx\Controller;

use Xcx\Common\ErrorCode;
use Xcx\Model\GoodsCouponModel;
use Xcx\Model\UserCouponModel;

class CouponController extends BaseController {

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取可以被兑换的优惠券列表
     */
    public function getGoodsCouponList()
    {
        $GoodsCoupon = new GoodsCouponModel();
        $list = $GoodsCoupon->getGoodsCouponList();
        $arr['code'] = 0;
        $arr['list'] = $list;
        $this->ajaxReturn($arr);
    }

    /**
     * 获取用户优惠券列表
     */
    public function getUserGoodsCouponList()
    {
        $UserCoupon = new UserCouponModel();
        $list = $UserCoupon->getUserGoodsCouponList();
        if ($list) {
            for ($i = 0; $i < count($list); $i ++) {
                $list[$i]['start_date'] = date('Y-m-d',$list[$i]['start_date']);
                $list[$i]['end_date'] = date('Y-m-d',$list[$i]['end_date']);
            }
        }
        $arr['code'] = 0;
        $arr['list'] = $list;
        $this->ajaxReturn($arr);
    }

    /**
     *
     */
    public function receiveGoodsCoupons()
    {
        $id = I('id');
        $UserCoupon = new UserCouponModel();
        $res = $UserCoupon->receiveGoodsCoupons($id);
        if ($res) {
            $arr['code'] = 0;
        }  else {
            $arr = array('code'=>$UserCoupon->getErrCode(),'msg'=>$UserCoupon->getErrMsg());
        }

        $this->ajaxReturn($arr);
    }

}